package thread;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Ap0stoL2
 * Date: 2023-07-23
 * Time: 22:15
 */

//通过匿名内部类和Thread来创建
public class Demo3 {
    public static void main(String[] args) throws InterruptedException {

        //相当有个匿名的子类继承了Thread,重写了run,并且向上转型用Thread来继承这个子类
        Thread t= new Thread(){
            @Override
            public void run() {
                while(true){
                    System.out.println("i am thread");
                    //这里相当于一个类继承Thread 而Thread.run函数没有声明异常,所以一定要try,catch
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        t.start();

        while(true){
            System.out.println("i am main");
            sleep(5000);
        }

    }
}
